<?php defined('SYSPATH') or die(); ?>
<!doctype html>
<html>
<head table_border=Haf0Aj >
    <meta charset="utf-8">
    <title>思途CMS{$coreVersion}</title>
    {template 'stourtravel/public/public_js'}
    {php echo Common::getCss('style.css,base.css,base_new.css'); }
    {php echo Common::getScript("uploadify/jquery.uploadify.min.js,jquery.buttonbox.js,choose.js,laydate/laydate.js"); }
    {php echo Common::getCss('uploadify.css','js/uploadify/'); }
</head>
<body style="overflow:hidden">

<table class="content-tab">
    <tr>
        <td width="119px" class="content-lt-td" valign="top">
            {template 'stourtravel/public/leftnav'}
            <!--右侧内容区-->
        </td>
        <td valign="top" class="content-rt-td" style="overflow:hidden">
            <div class="cfg-header-bar">
                <span class="fl">
                    <div class="choose-start-date mt-5 ml-5 w100" style="background: #fff">
                        <input type="text" class="date-text w100 start_date" placeholder="开始时间">
                        <i class="date-icon"></i>
                    </div>
                    <span class="item-text"> 至</span>
                    <div class="choose-start-date mt-5 ml-5 w100" style="background: #fff">
                        <input type="text" class="date-text w100 end_date" placeholder="结束时间">
                        <i class="date-icon"></i>
                    </div>
                </span>
                <span class="select-box w100 fl mt-5 ml-10">
                    <select name="status" class="select" onchange="promo_search()">
                        <option value="0">活动状态</option>
                        <option value="1">未开始</option>
                        <option value="2">进行中</option>
                        <option value="3">已结束</option>
                    </select>
                </span>
                <span class="select-box w100 fl mt-5 ml-10">
                    <select name="type" class="select" onchange="promo_search()">
                        <option value="0">活动内容</option>
                        <option value="1">单图固定广告</option>
                        <option value="2">多图轮播广告</option>
                        <option value="3">优惠券</option>
                        <option value="4">视频广告</option>
                    </select>
                </span>
                <div class="cfg-header-search">
                    <input type="text" id="searchkey" placeholder="活动名称" class="search-text">
                    <a href="javascript:;" class="search-btn" onclick="promo_search()">搜索</a>
                </div>
                <a href="javascript:;" class="fr btn btn-primary radius mt-6 mr-10" onclick="window.location.reload()">刷新</a>
                <a href="javascript:;" class="fr btn btn-primary radius mt-6 mr-10" id="addbtn">添加</a>
            </div>
            <div id="product_grid_panel" class="content-nrt">

            </div>
        </td>
    </tr>
</table>
<script>
    $(function(){
        laydate.render({
            elem: '.start_date'
        });
        laydate.render({
            elem: '.end_date'
        });
    })
    window.display_mode = 1;	//默认显示模式
    window.product_kindid = 0;  //默认目的地ID
    Ext.onReady(
        function () {
            Ext.tip.QuickTipManager.init();
            $(".btnbox").buttonBox();
            $("#searchkey").focusEffect();
            //添加按钮
            $("#addbtn").click(function () {
                ST.Util.addTab('添加活动', '{$cmsurl}promo/admin/promo/add_promo/{if isset($_GET['menuid'])}menuid/{$_GET['menuid']}/{/if}parentkey/article/itemid/4', 0);
            });
            //产品store
            window.product_store = Ext.create('Ext.data.Store', {
                fields: [
                    'id',
                    'title',
                    'start_date',
                    'end_date',
                    'type_name',
                    'status_name',
                    'click_peo_num',
                    'click_num',
                    'is_open',
                ],
                proxy: {
                    type: 'ajax',
                    extraParams: {
                        webid: '0'
                    },
                    api: {
                        read: SITEURL + 'promo/admin/promo/ad_list/action/read',  //读取数据的URL
                        update: SITEURL + 'promo/admin/promo/ad_list/action/save',
                        destroy: SITEURL + 'promo/admin/promo/ad_list/action/delete'
                    },
                    reader: {
                        type: 'json',   //获取数据的格式
                        root: 'lists',
                        totalProperty: 'total'
                    }
                },
                remoteSort: true,
                pageSize: 20,
                autoLoad: true,
                listeners: {
                    load: function (store, records, successful, eOpts) {
                        if (!successful) {
                            ST.Util.showMsg("{__('norightmsg')}", 5, 1000);
                        }
                        var pageHtml = ST.Util.page(store.pageSize, store.currentPage, store.getTotalCount(), 10);
                        $("#line_page").html(pageHtml);
                        window.product_grid.doLayout();

                        $(".pageContainer .pagePart a").click(function () {
                            var page = $(this).attr('page');
                            product_store.loadPage(page);
                        });
                    }
                }
            });

            //产品列表
            window.product_grid = Ext.create('Ext.grid.Panel', {
                store: product_store,
                renderTo: 'product_grid_panel',
                border: 0,
                width: "100%",
                bodyBorder: 0,
                bodyStyle: 'border-width:0px',
                scroll: 'vertical',
                bbar: Ext.create('Ext.toolbar.Toolbar', {
                    store: product_store,  //这个和grid用的store一样
                    displayInfo: true,
                    emptyMsg: "",
                    items: [
                        {
                            xtype: 'panel',
                            id: 'listPagePanel',
                            html: '<div id="line_page"></div>'
                        },
                        {
                            xtype: 'combo',
                            fieldLabel: '每页显示数量',
                            width: 170,
                            labelAlign: 'right',
                            forceSelection: true,
                            value: 20,
                            store: {fields: ['num'], data: [{num: 20}, {num: 40}, {num: 60}]},
                            displayField: 'num',
                            valueField: 'num',
                            listeners: {
                                select: CHOOSE.changeNum
                            }
                        }
                    ],
                    listeners: {
                        single: true,
                        render: function (bar) {
                            var items = this.items;
                            bar.insert(0, Ext.create('Ext.panel.Panel', {
                                border: 0,
                                html: '<div class="panel_bar">' +
                                    '<a class="btn btn-primary radius" href="javascript:void(0);" onclick="CHOOSE.chooseAll()">全选</a>' +
                                    '<a class="btn btn-primary radius ml-10" href="javascript:void(0);" onclick="CHOOSE.chooseDiff()">反选</a>' +
                                    '<a class="btn btn-primary radius ml-10" href="javascript:void(0);" onclick="CHOOSE.delMore()">删除</a>' +
                                    '</div>'
                            }));
                            bar.insert(1, Ext.create('Ext.panel.Panel', {border:0,items:[{
                                    xtype: 'button',
                                    cls:'my-extjs-btn',
                                    text: '批量设置',
                                    menu: {
                                        cls:'menu-no-icon',
                                        width:"82px",
                                        shadow:false,
                                        items:[
                                            {
                                                text: '开启',handler: function () {
                                                    let num=0
                                                    $(".product_check:checked").each(function(index,element){
                                                        num++;
                                                    });
                                                    if(num>50){
                                                        ST.Util.showMsg("最多选择50条！",5,2000);
                                                        return false
                                                    }
                                                    if(!num){
                                                        ST.Util.showMsg("请选择！",5,2000);
                                                        return false
                                                    }
                                                    $(".product_check:checked").each(function(index,element){
                                                        var id=$(element).val();
                                                        updateField(null,id,'is_open',1)
                                                    });
                                                }
                                            },
                                            {
                                                text: '关闭',handler: function () {
                                                    let num=0
                                                    $(".product_check:checked").each(function(index,element){
                                                        num++;
                                                    });
                                                    if(num>50){
                                                        ST.Util.showMsg("最多选择50条！",5,2000);
                                                        return false
                                                    }
                                                    if(!num){
                                                        ST.Util.showMsg("请选择！",5,2000);
                                                        return false
                                                    }
                                                    $(".product_check:checked").each(function(index,element){
                                                        var id=$(element).val();
                                                        updateField(null,id,'is_open',0)
                                                    });
                                                }
                                            },
                                        ]
                                    }
                                }]}
                            ));
                            bar.insert(2, Ext.create('Ext.toolbar.Fill'));
                        }
                    }
                }),
                columns: [
                    {
                        text: '选择',
                        width: '5%',
                        tdCls: 'product-ch',
                        align: 'center',
                        dataIndex: 'id',
                        sortable: false,
                        border: 0,
                        menuDisabled: true,
                        renderer: function (value, metadata, record) {
                            return "<input type='checkbox' class='product_check' style='cursor:pointer' value='" + value + "'/>";
                        }
                    },
                    {
                        text: '活动编号',
                        width: '6%',
                        tdCls: 'product-ch',
                        align: 'center',
                        sortable: false,
                        dataIndex: 'id',
                        border: 0,
                        menuDisabled: true,
                        renderer: function (value, metadata, record) {
                            return value;
                        }
                    },
                    {
                        text: '活动名称',
                        width: '20%',
                        tdCls: 'product-ch',
                        align: 'center',
                        sortable: false,
                        dataIndex: 'title',
                        border: 0,
                        menuDisabled: true,
                        renderer: function (value, metadata, record) {
                            return value;
                        }
                    },
                    {
                        text: '活动时间',
                        width: '15%',
                        tdCls: 'product-ch',
                        align: 'center',
                        sortable: false,
                        dataIndex: 'start_date',
                        border: 0,
                        menuDisabled: true,
                        renderer: function (value, metadata, record) {
                            var end_date = record.get('end_date');
                            return value+' 至 '+end_date;
                        }
                    },
                    {
                        text: '活动内容',
                        width: '15%',
                        tdCls: 'product-ch',
                        align: 'center',
                        sortable: false,
                        dataIndex: 'type_name',
                        border: 0,
                        menuDisabled: true,
                        renderer: function (value, metadata, record) {
                            return value;
                        }
                    },
                    {
                        text: '点击人数',
                        width: '12%',
                        tdCls: 'product-ch',
                        align: 'center',
                        sortable: false,
                        dataIndex: 'click_peo_num',
                        border: 0,
                        menuDisabled: true,
                        renderer: function (value, metadata, record) {
                            return value;
                        }
                    },
                    {
                        text: '点击次数',
                        width: '12%',
                        tdCls: 'product-ch',
                        align: 'center',
                        sortable: false,
                        dataIndex: 'click_num',
                        border: 0,
                        menuDisabled: true,
                        renderer: function (value, metadata, record) {
                            return value;
                        }
                    },
                    {
                        text: '开启/关闭',
                        width: '7%',
                        tdCls: 'product-ch',
                        align: 'center',
                        sortable: false,
                        dataIndex: 'is_open',
                        border: 0,
                        menuDisabled: true,
                        renderer: function (value, metadata, record) {
                            var id = record.get('id');
                            if(value==1) {
                                return '<img title="上架" alt="上架" onclick="updateField(null,'+id+',\'is_open\',0)" role="button" ' +
                                    ' src="" ' +
                                    'class="x-action-col-icon x-action-col-0 dest-status-ok">'
                            }
                            if(value==0){
                                return '<img title="下架" alt="下架" onclick="updateField(null,'+id+',\'is_open\',1)" role="button" ' +
                                    ' src="" ' +
                                    'class="x-action-col-icon x-action-col-0 dest-status-none">'
                            }
                        },
                        listeners:{
                            afterrender:function(obj,eopts)
                            {
                                if(window.display_mode!=1)
                                    obj.hide();
                                else
                                    obj.show();

                            }
                        }
                    },
                    {
                        text: '管理',
                        width: '8%',
                        align: 'center',
                        border: 0,
                        sortable: false,
                        menuDisabled: true,
                        renderer: function (value, metadata, record) {
                            var id = record.get('id');
                            var title = record.get('title');
                            return "<a href='javascript:void(0);' class='btn-link' data-title='"+title+"' onclick=\"goModify('" + id + "',$(this))\">编辑</a>";
                        }
                    }
                ],
                plugins: [
                    Ext.create('Ext.grid.plugin.CellEditing', {
                        clicksToEdit: 2,
                        listeners: {
                            edit: function (editor, e) {
                                var id = e.record.get('id');
                                updateField(0, id, e.field, e.value, 0);
                                return false;

                            },
                            beforeedit: function (editor, e) {

                            }
                        }
                    })
                ],
                viewConfig: {
                }
            });
        })
    //实现动态窗口大小
    Ext.EventManager.onWindowResize(function () {
        var height = Ext.dom.Element.getViewportHeight();
        window.product_grid.maxHeight = (height - 106);
        window.product_grid.doLayout();

    })

    function promo_search()
    {
        var keyword = $("#searchkey").val();
        var status = $("[name='status']").find('option:selected').val();
        var type = $("[name='type']").find('option:selected').val();
        var start_date = $(".start_date").val();
        var end_date = $(".end_date").val();
        window.product_store.getProxy().setExtraParam('keyword',keyword);
        window.product_store.getProxy().setExtraParam('status',status);
        window.product_store.getProxy().setExtraParam('type',type);
        window.product_store.getProxy().setExtraParam('start_date',start_date);
        window.product_store.getProxy().setExtraParam('end_date',end_date);
        window.product_store.loadPage(1);
    }

    //更新某个字段
    function updateField(ele, id, field, value, type, callback) {
        var record = window.product_store.getById(id.toString());
        if (type == 'select' || type == 'input') {
            value = Ext.get(ele).getValue();
        }

        Ext.Ajax.request({
            url: "admin/promo/ad_list/action/update",
            method: "POST",
            datatype: "JSON",
            params: {id: id, field: field, val: value, kindid: window.product_kindid},
            success: function (response, opts) {
                var data = JSON.parse(response.responseText);
                if (data.status) {
                    record.set(field, value);
                    record.commit();
                    if (typeof (callback) == 'function')
                        callback(record);
                }else{
                    ST.Util.showMsg(data.msg,5)
                }
            }
        });
    }

    //修改
    function goModify(id,obj) {
        var title = obj.attr('data-title');
        var url = SITEURL + 'promo/admin/promo/edit/{if isset($_GET['menuid'])}menuid/{$_GET['menuid']}/{/if}promo_id/' + id;
        parent.window.addTab('修改：'+title, url, 1);
    }
</script>
</body>
</html>
